package com.ruoyi.fun.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.fun.domain.EcAnnouncement;
import com.ruoyi.fun.service.IEcAnnouncementService;
import com.ruoyi.fun.service.IEcCategoryService;
import com.ruoyi.fun.service.IEcProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 测试数据控制器
 */
@RestController
@RequestMapping("/test")
public class TestDataController extends BaseController {
    
    /**
     * 检查数据库中的组织ID数据 - 无需认证版本
     */
    @GetMapping("/checkOrgDataPublic")
    public AjaxResult checkOrgDataPublic() {
        try {
            
            return success("数据检查完成，请查看控制台日志");
            
        } catch (Exception e) {
            System.out.println("检查数据时出错: " + e.getMessage());
            e.printStackTrace();
            return error("检查数据失败: " + e.getMessage());
        }
    }
    
    @Autowired
    private IEcCategoryService ecCategoryService;
    
    @Autowired
    private IEcProductService ecProductService;
    
    @Autowired
    private IEcAnnouncementService ecAnnouncementService;
    
    /**
     * 检查数据库中的组织ID数据
     */
    @GetMapping("/checkOrgData")
    public AjaxResult checkOrgData() {
        try {
            // 检查当前用户信息
            Long currentOrgId = getOrgId();
            Long currentUserId = getUserId();
            Long currentDeptId = getDeptId();
            
            System.out.println("=== 当前用户信息 ===");
            System.out.println("用户ID: " + currentUserId);
            System.out.println("部门ID: " + currentDeptId);
            System.out.println("组织ID (getOrgId): " + currentOrgId);
            
            // 检查公告数据
            System.out.println("=== 检查公告数据 ===");
            EcAnnouncement queryParam = new EcAnnouncement();
            queryParam.setOrgId(currentOrgId);
            List<EcAnnouncement> announcements = ecAnnouncementService.selectEcAnnouncementList(queryParam);
            System.out.println("当前组织(" + currentOrgId + ")的公告数量: " + announcements.size());
            
            for (EcAnnouncement announcement : announcements) {
                System.out.println("公告ID: " + announcement.getAnnouncementId() + ", 标题: " + announcement.getTitle() + ", 组织ID: " + announcement.getOrgId());
            }
            
            // 检查特定公告ID=3的数据
            System.out.println("=== 检查公告ID=3的数据 ===");
            EcAnnouncement announcement3 = ecAnnouncementService.selectEcAnnouncementByAnnouncementIdAndOrgId(3L, currentOrgId);
            if (announcement3 != null) {
                System.out.println("公告ID=3存在于当前组织: " + announcement3.getTitle() + ", 组织ID: " + announcement3.getOrgId());
            } else {
                System.out.println("公告ID=3不存在于当前组织(" + currentOrgId + ")");
                // 检查公告ID=3是否存在于其他组织
                EcAnnouncement announcement3Any = ecAnnouncementService.selectEcAnnouncementByAnnouncementId(3L);
                if (announcement3Any != null) {
                    System.out.println("公告ID=3存在于组织: " + announcement3Any.getOrgId() + ", 标题: " + announcement3Any.getTitle());
                } else {
                    System.out.println("公告ID=3在数据库中不存在");
                }
            }
            
            return success("数据检查完成，请查看控制台日志");
            
        } catch (Exception e) {
            System.out.println("检查数据时出错: " + e.getMessage());
            e.printStackTrace();
            return error("检查数据失败: " + e.getMessage());
        }
    }
}